package com.adamfass.jmetest;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingSphere;
import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Line;
import com.jme.math.Quaternion;
import com.jme.math.Ring;
import com.jme.math.TransformMatrix;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Cylinder;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.util.TextureManager;
import com.jme.util.Timer;
import com.jmex.effects.particles.ParticleFactory;
import com.jmex.effects.particles.ParticleInfluence;
import com.jmex.effects.particles.ParticleMesh;
import com.jmex.effects.particles.SimpleParticleInfluenceFactory;

public class MovingVortex extends SimpleGame {
  
  private ParticleMesh pMesh;
  private Vector3f currentPos = new Vector3f(), newPos = new Vector3f();
  private float frameRate = 0;
  private Node node;

  public void simpleInitGame() {
    node = new Node();
    
    display.setTitle("Particle System");
    lightState.setEnabled(false);

    AlphaState as1 = display.getRenderer().createAlphaState();
    as1.setBlendEnabled(true);
    as1.setSrcFunction(AlphaState.SB_SRC_ALPHA);
    as1.setDstFunction(AlphaState.DB_ONE);
    as1.setTestEnabled(true);
    as1.setTestFunction(AlphaState.TF_GREATER);
    as1.setEnabled(true);

    TextureState ts = display.getRenderer().createTextureState();
    ts.setTexture(
        TextureManager.loadTexture(
        MovingVortex.class.getClassLoader().getResource(
        "com/adamfass/jmetest/flaresmall.jpg"),
        Texture.MM_LINEAR_LINEAR,
        Texture.FM_LINEAR));
    ts.setEnabled(true);
    
//    Cylinder cylinder = new Cylinder("", 10, 10, 1, 1);
//    rootNode.attachChild(cylinder);
//    rootNode.updateRenderState();

//    pMesh = ParticleFactory.buildBatchParticles("particles", cylinder.getBatch(0));
    pMesh = ParticleFactory.buildParticles("particles", 300);
    pMesh.setEmissionDirection(new Vector3f(0,1,0));
    pMesh.setInitialVelocity(.002f);
    pMesh.setStartSize(0.2f);
    pMesh.setEndSize(0.2f);
    pMesh.setMinimumLifeTime(1000f);
    pMesh.setMaximumLifeTime(1000f);
    pMesh.setStartColor(new ColorRGBA(0.5f, 1, 0.5f, 1));
    pMesh.setEndColor(new ColorRGBA(0, 1, 0, 1));
    pMesh.setMaximumAngle(10f * FastMath.DEG_TO_RAD);
    pMesh.getParticleController().setControlFlow(true);
    pMesh.setGeometry(new Line(Vector3f.ZERO, Vector3f.UNIT_Y.multLocal(5)));
    
    
    ParticleInfluence vortex = SimpleParticleInfluenceFactory.createBasicVortex(
            0.04f, 0, new Line(Vector3f.ZERO, Vector3f.UNIT_Y.multLocal(5)), 
            true, true);
    vortex.setEnabled(true);
    pMesh.addInfluence(vortex);
    
    pMesh.warmUp(300);

    node.setRenderState(ts);
    node.setRenderState(as1);
    ZBufferState zstate = display.getRenderer().createZBufferState();
    zstate.setEnabled(false);
    pMesh.setRenderState(zstate);
    pMesh.setModelBound(new BoundingSphere());
    pMesh.updateModelBound();

    rootNode.attachChild(node);
    node.attachChild(pMesh);
  }
  
  protected void simpleUpdate() {
    float x = 2 * FastMath.sin(Timer.getTimer().getTimeInSeconds());
//    pMesh.setEmitterTransform(
//            new TransformMatrix(new Quaternion(), new Vector3f(x, 0, 0)));
//    pMesh.setLocalTranslation(x, 0, 0);
//    pMesh.updateGeometricState(Timer.getTimer().getTimeInSeconds(), false);
    
//    pMesh.getInfluences().get(0).setAxis()
    
//    pMesh.setLocalTranslation(x, 0, 0);
//    pMesh.clearInfluences();
//    pMesh.forceRespawn()
//    ParticleInfluence vortex = SimpleParticleInfluenceFactory.createBasicVortex(
//            0.04f, 0, 
//            new Line(new Vector3f(x, 0, 0), new Vector3f(x, 5, 0)), 
//            false, true);
//    vortex.setEnabled(true);
//    pMesh.addInfluence(vortex);
    // amf!!! remove and add vortex influence for new position

  }
  
  /**
   * @param args
   */
  public static void main(String[] args) {
    MovingVortex app = new MovingVortex();
    app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
    app.start();
  }

}
